
//询问是否跳转至登录页
const inquiryToLogin = () => {
	return new Promise ((resolve, reject) => {
		if(uni.getStorageSync('token')) {
			resolve(uni.getStorageSync('token'));
		}else {
			// reject('登录过时：' + getCurrentPages()[getCurrentPages().length - 1].route)
			uni.showModal({
				title: '提示', content: '登录过时，是否立即跳转登录？',
				confirmColor: '#64aeb4',
				success:res => {
					if (res.confirm) uni.navigateTo({ url: '/pages/personalCenter/login' })
				}
			})
		}
	})
}

/**
 * 获取日期，如：2022-12-09
 * @param {String} type:返回加或减去多少的时间
 * @param {Number} data:时间戳
 * @param {Boolean} notYear:若传 true 则不要年份
 */
const dateLimit = (type, data, notYear) => {
	let date = '',
		showStr = '';
	if(data) {
		date = data;
	}else {
		date = new Date();
	}
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	if (type === 'start') {
		year = year + 0;
	} else if (type === 'end') {
		// year = year + 1;
		// month = month + 1;
		day = day + 7;
	} else if(type === 'oldYear1') {
		year = year - 1;
	}
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	if(notYear) {
		showStr = `${month}.${day}`;
	}else {
		showStr = `${year}-${month}-${day}`;
	}
	return showStr;
}

const pathToBase64 = (path) => {
	return new Promise(function(resolve, reject) {
	    if (typeof window === 'object' && 'document' in window) {
	        if (typeof FileReader === 'function') {
	            var xhr = new XMLHttpRequest()
	            xhr.open('GET', path, true)
	            xhr.responseType = 'blob'
	            xhr.onload = function() {
	                if (this.status === 200) {
	                    let fileReader = new FileReader()
	                    fileReader.onload = function(e) {
	                        resolve(e.target.result)
	                    }
	                    fileReader.onerror = reject
	                    fileReader.readAsDataURL(this.response)
	                }
	            }
	            xhr.onerror = reject
	            xhr.send()
	            return
	        }
	        var canvas = document.createElement('canvas')
	        var c2x = canvas.getContext('2d')
	        var img = new Image
	        img.onload = function() {
	            canvas.width = img.width
	            canvas.height = img.height
	            c2x.drawImage(img, 0, 0)
	            resolve(canvas.toDataURL())
	            canvas.height = canvas.width = 0
	        }
	        img.onerror = reject
	        img.src = path
	        return
	    }
	    if (typeof plus === 'object') {
	        plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
	            entry.file(function(file) {
	                var fileReader = new plus.io.FileReader()
	                fileReader.onload = function(data) {
	                    resolve(data.target.result)
	                }
	                fileReader.onerror = function(error) {
	                    reject(error)
	                }
	                fileReader.readAsDataURL(file)
	            }, function(error) {
	                reject(error)
	            })
	        }, function(error) {
	            reject(error)
	        })
	        return
	    }
	    if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
	        wx.getFileSystemManager().readFile({
	            filePath: path,
	            encoding: 'base64',
	            success: function(res) {
	                resolve('data:image/png;base64,' + res.data)
	            },
	            fail: function(error) {
	                reject(error)
	            }
	        })
	        return
	    }
	    reject(new Error('not support'))
	})
}
const formatRichText = (html) => { // 富文本内容格式化
	if(!html) return '';
	return html && html.replace(/<img[^>]*>/gi, function(match, capture) { // 查找所有的 img 元素
		return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi, '') // 删除找到的所有 img 元素中的 style 属性
	}).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性，并设置宽度为 100%
}

export {
	inquiryToLogin, dateLimit, pathToBase64, formatRichText
}